﻿// ———————————————————————–
// <copyright file="ValueListService.svc.cs" company="The MITRE Corporation">
//    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// ValueListService.svc.cs - "Interfaces With The DAL To Update, Obtain, Or Delete ValueLists."
// Project: "IC.NETSOA"- "ICNETREST"
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2010
// Author:      Don McGarry The MITRE Corporation
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)
                
using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Xml.Linq;
using ICNETDAL;

namespace ICNETREST
{
  /// <summary>
  /// Communicates with the DAL to modify ValueLists or their contents. 
  /// </summary>
  [ServiceContract(Namespace = "urn:icnet:mitre:org:ValueListService")]
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
  public class ValueListService
  {
    /// <summary>
    /// Gets the content keywords stored in the database.
    /// </summary>
    /// <returns>An XML object containing the content keywords.</returns>
    [WebGet(UriTemplate = "/ContentKeywords", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare), OperationContract]
    public XElement GetContentKeywords()
    {
      XElement xe = ValueListDAL.GetValuesByListName("http://icnet.mitre.org/ValueLists/ContentKeywords");
      return xe;
    }

    /// <summary>
    /// Get all of the roles.
    /// </summary>
    /// <returns>An XML object containing all of the roles.</returns>
    [WebGet(UriTemplate = "/Roles", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare), OperationContract]
    public XElement GetRoles()
    {
        XElement xe = ValueListDAL.GetListNamesWithPattern("/ValueLists/Roles/");
        return xe;
    }

    /// <summary>
    /// Get all of the roles in a certain ValueList.
    /// </summary>
    /// <param name="listName">The value list to search for.</param>
    /// <returns>An XML object containing all of the roles for a certain value list.</returns>
    [WebGet(UriTemplate = "/Roles/{ListName}", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare), OperationContract]
    public XElement GetRolesByListName(string listName)
    {
      XElement xe = ValueListDAL.GetValuesByListName("http://icnet.mitre.org/ValueLists/Roles/" + listName);
      return xe;
    }

    /// <summary>
    /// Gets the names of all of the ValueLists.
    /// </summary>
    /// <returns>An XML object containing the names of all of the ValueLists.</returns>
    [WebGet(UriTemplate = "/", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare), OperationContract]
    public XElement GetListNames()
    {
      XElement xe = ValueListDAL.GetListNames();
      return xe;
    }

    /// <summary>
    /// Gets all of the ValueLists.
    /// </summary>
    /// <returns>An XML object containing all of the ValueLists.</returns>
    [WebGet(UriTemplate = "/ValueList", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare), OperationContract]
    public XElement GetValueLists()
    {
      XElement xe = ValueListDAL.GetValueLists();
      return xe;
    }

    /// <summary>
    /// Adds a new ValueList to the database.
    /// </summary>
    /// <param name="xe">The ValueList to be added.</param>
    /// <returns>An XML object containing the ValueListand its ID, or an error if it was not sucesfully created.</returns>
    [WebInvoke(UriTemplate = "/ValueList", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    public XElement CreateNewValueList(XElement xe)
    {
      XElement result = ValueListDAL.CreateValueList(xe);
      return result;
    }

    /// <summary>
    /// Modifies a ValueList in the database.
    /// </summary>
    /// <param name="xe">The ValueList to be modified.</param>
    /// <returns>An XML object containing the ValueListand its ID, or an error if it was not sucesfully created.</returns>
    [WebInvoke(UriTemplate = "ValueList/{Id}", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    public XElement ModifyValueList(XElement xe, string id)
    {
        try
        {
            System.Guid gid = System.Guid.Parse(id);
            XElement result = ValueListDAL.ModifyValueList(xe, gid);
            return result;
        }
        catch (FormatException)
        {
        }

        return new XElement("ValueList", null);
    }


    /// <summary>
    /// Looks up a ValueList in the databaes.
    /// </summary>
    /// <param name="id">The ID that is used to look up the database.</param>
    /// <returns> An XML object containing the matching ValueList.</returns>
    [WebGet(UriTemplate = "/ValueList/{Id}", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare), OperationContract]
    public XElement GetValueListByID(string id)
    {
      try
      {
        System.Guid gid = new Guid(id);
        XElement xe = ValueListDAL.GetValueList(gid);
        return xe;
      }
      catch (FormatException)
      {
      }

      return new XElement("ValueList", null);
    }

    /// <summary>
    /// Deletes a ValueList from the database.
    /// </summary>
    /// <param name="id">The ID of the ValueList to be deleted.</param>
    /// <returns>An XML object containing the ID of the ValueList and its ID in one element.</returns>
    [WebInvoke(UriTemplate = "ValueList/{Id}", Method = "DELETE", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare), OperationContract]
    public XElement DeleteValueListByID(string id)
    {
      try
      {
        System.Guid gid = System.Guid.Parse(id);
        ValueListDAL.DeleteValueList(gid);
      }
      catch (FormatException)
      {
      }

      return new XElement("ValueList", id);
    }
  }
}

